package com.zzyl.serve.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.DeviceData;
import com.zzyl.serve.domain.Room;
import com.zzyl.serve.mapper.RoomMapper;
import com.zzyl.serve.service.IRoomService;
import com.zzyl.serve.vo.BedVo;
import com.zzyl.serve.vo.DeviceInfo;
import com.zzyl.serve.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author liuyp
 * @date 2025-07-30
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getRoomById(Long id) {
        return roomMapper.getRoomById(id);
    }

    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //1. 执行SQL语句，查询房间及智能设备（没有查询设备的数据）
        List<RoomVo> list = roomMapper.getRoomsWithDeviceByFloorId(floorId);

        //2. 再从Redis中获取每个设备的数据
        for (RoomVo roomVo : list) {
            //2.1 每个房间可能有多个设备，循环处理每个设备
            List<DeviceInfo> roomDevices = roomVo.getDeviceVos();
            for (DeviceInfo roomDevice : roomDevices) {
                //要从Redis里取此设备的数据。使用的是hash结构，大key在CacheConstants常量类里，小key是设备的id，value值是最新的数据
                String roomDeviceDataArrayJson = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, roomDevice.getIotId());
                //将json格式的数据转换成对象：DeviceData
                List<DeviceData> roomDeviceDataList = JSONUtil.toList(roomDeviceDataArrayJson, DeviceData.class);
                //将设备的数据设置给当前房间设备对象
                roomDevice.setDeviceDataVos(roomDeviceDataList);
            }
            //2.2 每个房间里的各个床位，每个床位可能有设备，要从Redis里取此设备的数据
            List<BedVo> bedVoList = roomVo.getBedVoList();
            for (BedVo bedVo : bedVoList) {
                //获取此床位绑定的设备，可能有多个
                List<DeviceInfo> bedDevices = bedVo.getDeviceVos();
                //处理此床位的每个设备
                for (DeviceInfo bedDevice : bedDevices) {
                    //从Redis里获取此设备的数据
                    String bedDeviceDataArrayJson = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, bedDevice.getIotId());
                    //将json格式的数据转换成对象：DeviceData
                    List<DeviceData> bedDeviceDataList = JSONUtil.toList(bedDeviceDataArrayJson, DeviceData.class);
                    //将设备的数据设置给当前床位
                    bedDevice.setDeviceDataVos(bedDeviceDataList);
                }
            }
        }

        return list;
    }
}
